/*
 * INSANE - Interactive Structural Analysis Environment
 *
 * Copyright (C) 2003-2004
 * Universidade Federal de Minas Gerais
 * Escola de Engenharia
 * Departamento de Engenharia de Estruturas
 *
 * Author's email :     insane@dees.ufmg.br
 * Author's Website :   http://www.dees.ufmg.br/insane
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
 * 1307, USA.
 *
 */

package br.ufmg.dees.insane.ui.rich.postp.postp_view.dialog;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;

import br.ufmg.dees.insane.commons.view.ViewState;
import br.ufmg.dees.insane.model.postp.PostpModel;
import br.ufmg.dees.insane.model.postp.StepPostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_controller.PostpController;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.DesktopPostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.InterfacePostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.PostpViewState;

public class ElementsContoursSelectionDialog extends JFrame {
	
	/**   */
	private static final long serialVersionUID = 1L;
	
	private DesktopPostp desktop;
	private InterfacePostp intFrame;
	private PostpViewState state;
	private PostpModel postpModel;
	private StepPostp stepPostp;
	
	private JList panelList;
	
	private JPanel contentPane; 
	
	private String[] precisionStr = {"0", "0.0", "0.00", "0.000", "0.0000", "0.00000", "0.000000", "0.0000000", "0.00000000"
			,"0.000000000","0.0000000000","0.00000000000","0.000000000000"};
	private JComboBox precision = new JComboBox(precisionStr);
	
	private JButton copyButton1;
	private JButton setColorsButton;
	private JButton viewButton;
	private JButton okButton;
	private JButton cancelButton;
	private JTextField textField1;
	private JCheckBox openNewWindowCheckBox = new JCheckBox("Open In a New Window");
	private JCheckBox onGaussCheckBox ;
	private JCheckBox onFullMeshCheckBox;
	private JCheckBox onMeshCheckBox;
	private String[] keysList;
	private JScrollPane scroll;
	
	private ElementsContoursSelectionDialog thisDlg;
	
	public ElementsContoursSelectionDialog(String[] keys, ViewState vs){
		super("Contours Range");
		this.keysList = new String[keys.length+1];
		for (int i = 0; i < this.keysList.length-1; i++) {
			this.keysList[i] = keys[i];
		}
		this.keysList[keysList.length-1] = ""; 
		this.state = (PostpViewState)vs;
		new Thread(new Runnable(){
			public void run() {
				init();
			}
		}).start();
	}
	
	public void init(){
		thisDlg = this;
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setTitle("Contours Range"+" - "+this.intFrame.getPostpModel().getLabel());
		Image ico = Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/Insane64.gif"));
		this.setIconImage(ico);
		//setting the createdSequencesPane
		JPanel listPane = new JPanel();
		listPane.setSize(new Dimension(500,750));
		listPane.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createTitledBorder("Value Selection"),
				BorderFactory.createEmptyBorder(2,2,2,2)));
		listPane.setLayout(new BoxLayout(listPane,BoxLayout.Y_AXIS)); 
		panelList = new JList(keysList); 
		panelList.setBackground(Color.BLACK);
		panelList.setForeground(Color.GREEN);
		panelList.setVisibleRowCount(5);
		panelList.setFixedCellHeight(20);
		panelList.setFixedCellWidth(300);
		panelList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		panelList.setAutoscrolls(true);
		scroll = new JScrollPane(panelList);
		listPane.add(scroll);
		scroll.setViewportView(panelList); 
		
		copyButton1 = new JButton(">> Copy >>");
		
		setColorsButton = new JButton("Palette");
		viewButton = new JButton("View");
		okButton = new JButton("    OK    ");
		cancelButton = new JButton("Cancel");
		
		textField1 = new JTextField();
		textField1.setText("");
		textField1.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createTitledBorder(""),
				BorderFactory.createEmptyBorder(2,2,2,2)));
		textField1.setBackground(Color.BLACK);
		textField1.setForeground(Color.GREEN);
		textField1.setEditable(false);
		textField1.setPreferredSize(new Dimension(150,20));
		
		precision.setSelectedIndex(state.getFracDigits());
		
		onGaussCheckBox = new JCheckBox("From Gauss values");
		onGaussCheckBox.setSelected(true);
		ActionListener onGaussListener = new ChoiceListenerOnGaussMesh();       
		onGaussCheckBox.addActionListener(onGaussListener);
		
		onFullMeshCheckBox = new JCheckBox("From Gauss and nodal values");
		onFullMeshCheckBox.setSelected(false);
		ActionListener onFullMeshListener = new ChoiceListenerOnFullMesh();       
		onFullMeshCheckBox.addActionListener(onFullMeshListener);
		
		onMeshCheckBox = new JCheckBox("From nodal values");
		onMeshCheckBox.setSelected(false);
		ActionListener onMeshListener = new ChoiceListenerOnMesh();       
		onMeshCheckBox.addActionListener(onMeshListener);
		
		contentPane = (JPanel)this.getContentPane(); 
		
		copyButton1.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						textField1.setText(addText(panelList));
					}
				});
		
		setColorsButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						new ColorsPalete32ChooserDialog((InterfacePostp)intFrame,state);
					}
				});
		
		viewButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						if(textField1.getText().equals("")){
							String message = "You must select a value!"; 
							JOptionPane.showMessageDialog(thisDlg, message, "Warning", JOptionPane.INFORMATION_MESSAGE);
						}else{
							View(event);
						}
					}
				});
		
		okButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						if(textField1.getText().equals("")){
							String message = "You must select a value!"; 
							JOptionPane.showMessageDialog(thisDlg, message, "Warning", JOptionPane.INFORMATION_MESSAGE);
						}else{
							approvedPerform();
							Close(event);
						}
						
					}
				});
		
		cancelButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						Close(event);
					}
				});
		
		// Adding components in the contentPane 
		contentPane.setLayout(null); 
		addComponent(contentPane, listPane, 5,5,240,240); 
		addComponent(contentPane, new JLabel("Value:"), 365,15,50,25);
		addComponent(contentPane, copyButton1, 255,45,100,25);
		addComponent(contentPane, textField1, 365,41,100,33);
		addComponent(contentPane, setColorsButton, 255,100,100,25);
		addComponent(contentPane, viewButton, 365,100,100,25);
		addComponent(contentPane, new JLabel("Precision:"), 255,140,75,25);
		addComponent(contentPane, precision, 350,140,120,25);
		addComponent(contentPane, onGaussCheckBox, 275,175,200,20);
		addComponent(contentPane, onFullMeshCheckBox, 275,200,200,20);
		addComponent(contentPane, onMeshCheckBox, 275,225,200,20);
		addComponent(contentPane, openNewWindowCheckBox, 10,260,200,20);
		addComponent(contentPane,okButton, 255,260,100,25);
		addComponent(contentPane,cancelButton, 365,260,100,25);
		
		this.setSize(485,325);
		this.setLocationRelativeTo(desktop);
		this.setResizable(false);
		// this.setAlwaysOnTop(true);
		this.setVisible(true);
		this.repaint();
		
	}
	
	public String addText(JList list){
		String text="";
		for(int i=0;i<list.getSelectedValues().length;i++){
			text+=((String)list.getSelectedValues()[i]);
		}
		return text;
	}
	
	/** Add Component Without a Layout Manager (Absolute Positioning) */ 
	private void addComponent(Container container,Component c,int x,int y,int width,int height) 
	{ 
		c.setBounds(x,y,width,height); 
		container.add(c); 
	} 
	
	public void Close(ActionEvent event){
		this.dispose();
	}
	
	public void Close(){
		this.dispose();
	}
	
	public void View(ActionEvent event){
		//this.state.setDeformedOn(true);
		if(this.openNewWindowCheckBox.isSelected()){
			InterfacePostp intFrame = new InterfacePostp();
			intFrame.validate();
			intFrame.setPostpModel(this.postpModel);
			intFrame.setModel(this.stepPostp);
			intFrame.setTitle(postpModel.getLabel());
			((DesktopPostp)this.desktop).addInternalFrame(intFrame);
			intFrame.toFront();
			this.state = (PostpViewState)((InterfacePostp)intFrame).getView().getViewState();
			this.state.getEdgeViewState().setLineColor(Color.WHITE);
			this.state.getNodalsPlotContoursViewState().setContourOnOff(false);
			this.state.getElementsPlotContoursViewState().setContourOnOff(true);
			this.state.getElementsPlotContoursViewState().setDiscontinuosContourOnOff(false);
			this.state.getElementsPlotContoursViewState().setElementsContoursComputed(true);
			this.state.getElementsPlotContoursViewState().setKeyValue(this.textField1.getText());
			if(onGaussCheckBox.isSelected()){
				this.state.getElementsPlotContoursViewState().setTriangulateWithNodesOnOff(false);
				this.state.getElementsPlotContoursViewState().setOnMesh(false);
			}
			if(onFullMeshCheckBox.isSelected()){
				this.state.getElementsPlotContoursViewState().setTriangulateWithNodesOnOff(true);
				this.state.getElementsPlotContoursViewState().setOnMesh(false);
			}
			if(onMeshCheckBox.isSelected()){
				this.state.getElementsPlotContoursViewState().setTriangulateWithNodesOnOff(false);
				this.state.getElementsPlotContoursViewState().setOnMesh(true);
			}
			this.state.setFracDigits(this.precision.getSelectedIndex());
			this.state.getScaleColorsViewState().setScaleContoursOnOff(true);
			((PostpController)((InterfacePostp)this.intFrame).getView().getController()).width = 2; 
			(intFrame).getView().getDrawingArea().repaint();    
		}else{
			this.state.getNodalsPlotContoursViewState().setContourOnOff(false);
			this.state.getElementsPlotContoursViewState().setContourOnOff(true);
			this.state.getElementsPlotContoursViewState().setDiscontinuosContourOnOff(false);
			this.state.getElementsPlotContoursViewState().setElementsContoursComputed(true);
			this.state = (PostpViewState)(((InterfacePostp)((DesktopPostp)this.desktop).getDesktop().getSelectedFrame())).getView().getViewState();
			this.state.getEdgeViewState().setLineColor(Color.WHITE);
			this.state.setFracDigits(this.precision.getSelectedIndex());
			this.state.getElementsPlotContoursViewState().setKeyValue(this.textField1.getText());
			if(onGaussCheckBox.isSelected()){
				this.state.getElementsPlotContoursViewState().setTriangulateWithNodesOnOff(false);
				this.state.getElementsPlotContoursViewState().setOnMesh(false);
			}
			if(onFullMeshCheckBox.isSelected()){
				this.state.getElementsPlotContoursViewState().setTriangulateWithNodesOnOff(true);
				this.state.getElementsPlotContoursViewState().setOnMesh(false);
			}
			if(onMeshCheckBox.isSelected()){
				this.state.getElementsPlotContoursViewState().setTriangulateWithNodesOnOff(false);
				this.state.getElementsPlotContoursViewState().setOnMesh(true);
			}
			this.state.getScaleColorsViewState().setScaleContoursOnOff(true);
			((PostpController)((InterfacePostp)this.intFrame).getView().getController()).width = 2; 
			((InterfacePostp)((DesktopPostp)this.desktop).getDesktop().getSelectedFrame()).getView().getDrawingArea().repaint();
		}
	}
	
	public void showErrMessage(ActionEvent event){
		JOptionPane.showMessageDialog(this, "Set the X and Y fields at least!", "Error Message", JOptionPane.ERROR_MESSAGE);
	}
	
	public void approvedPerform(){
		this.state.setDeformedOn(true);
		if(this.openNewWindowCheckBox.isSelected()){
			InterfacePostp intFrame = new InterfacePostp();
			intFrame.validate();
			intFrame.setPostpModel(this.postpModel);
			intFrame.setModel(this.stepPostp);
			intFrame.setTitle(postpModel.getLabel());
			((DesktopPostp)this.desktop).addInternalFrame(intFrame);
			intFrame.toFront();
			this.state.getNodalsPlotContoursViewState().setContourOnOff(false);
			this.state.getElementsPlotContoursViewState().setContourOnOff(true);
			this.state.getElementsPlotContoursViewState().setDiscontinuosContourOnOff(false);
			this.state.getElementsPlotContoursViewState().setElementsContoursComputed(true);
			this.state = (PostpViewState)((InterfacePostp)intFrame).getView().getViewState();
			this.state.getEdgeViewState().setLineColor(Color.WHITE);
			this.state.getElementsPlotContoursViewState().setContourOnOff(true);
			this.state.getElementsPlotContoursViewState().setKeyValue(this.textField1.getText());
			if(onGaussCheckBox.isSelected()){
				this.state.getElementsPlotContoursViewState().setTriangulateWithNodesOnOff(false);
				this.state.getElementsPlotContoursViewState().setOnMesh(false);
			}
			if(onFullMeshCheckBox.isSelected()){
				this.state.getElementsPlotContoursViewState().setTriangulateWithNodesOnOff(true);
				this.state.getElementsPlotContoursViewState().setOnMesh(false);
			}
			if(onMeshCheckBox.isSelected()){
				this.state.getElementsPlotContoursViewState().setTriangulateWithNodesOnOff(false);
				this.state.getElementsPlotContoursViewState().setOnMesh(true);
			}
			this.state.setFracDigits(this.precision.getSelectedIndex());
			this.state.getScaleColorsViewState().setScaleContoursOnOff(true);
			((PostpController)((InterfacePostp)this.intFrame).getView().getController()).width = 2; 
			(intFrame).getView().getDrawingArea().repaint();
		}else{
			this.state.getNodalsPlotContoursViewState().setContourOnOff(false);
			this.state.getElementsPlotContoursViewState().setContourOnOff(true);
			this.state.getElementsPlotContoursViewState().setDiscontinuosContourOnOff(false);
			this.state.getElementsPlotContoursViewState().setElementsContoursComputed(true);
			this.state = (PostpViewState)(((InterfacePostp)((DesktopPostp)this.desktop).getDesktop().getSelectedFrame())).getView().getViewState();
			this.state.getEdgeViewState().setLineColor(Color.WHITE);
			this.state.getElementsPlotContoursViewState().setContourOnOff(true);
			this.state.getElementsPlotContoursViewState().setKeyValue(this.textField1.getText());
			if(onGaussCheckBox.isSelected()){
				this.state.getElementsPlotContoursViewState().setTriangulateWithNodesOnOff(false);
				this.state.getElementsPlotContoursViewState().setOnMesh(false);
			}
			if(onFullMeshCheckBox.isSelected()){
				this.state.getElementsPlotContoursViewState().setTriangulateWithNodesOnOff(true);
				this.state.getElementsPlotContoursViewState().setOnMesh(false);
			}
			if(onMeshCheckBox.isSelected()){
				this.state.getElementsPlotContoursViewState().setTriangulateWithNodesOnOff(false);
				this.state.getElementsPlotContoursViewState().setOnMesh(true);
			}
			this.state.setFracDigits(this.precision.getSelectedIndex());
			this.state.getScaleColorsViewState().setScaleContoursOnOff(true);
			((PostpController)((InterfacePostp)this.intFrame).getView().getController()).width = 2; 
			((InterfacePostp)((DesktopPostp)this.desktop).getDesktop().getSelectedFrame()).getView().getDrawingArea().repaint();
		}
	}
	
	private class ChoiceListenerOnGaussMesh implements ActionListener{
		public void actionPerformed(ActionEvent event){
				onGaussCheckBox.setSelected(true);
				onFullMeshCheckBox.setSelected(false);
				onMeshCheckBox.setSelected(false);
		}    
	}
	
	private class ChoiceListenerOnFullMesh implements ActionListener{
		public void actionPerformed(ActionEvent event){
			onGaussCheckBox.setSelected(false);
			onFullMeshCheckBox.setSelected(true);
			onMeshCheckBox.setSelected(false);
		}    
	}
	
	private class ChoiceListenerOnMesh implements ActionListener{
		public void actionPerformed(ActionEvent event){
			onGaussCheckBox.setSelected(false);
			onFullMeshCheckBox.setSelected(false);
			onMeshCheckBox.setSelected(true);
		}    
	}
	
	
	/**
	 * The method return the intFrame.
	 * @return Returns The intFrame.
	 */
	public JInternalFrame getIntFrame() {
		return intFrame;
	}
	
	/**
	 * The method set the field intFrame from the parameter intFrame.
	 * @param intFrame The intFrame to set.
	 */
	public void setIntFrame(InterfacePostp intFrame) {
		this.intFrame = intFrame;
	}
	
	/**
	 * The method return the parent.
	 * @return Returns The parent.
	 */
	public DesktopPostp getDesktop() {
		return desktop;
	}
	
	/**
	 * The method set the field desktop from the parameter desktop.
	 * @param desktop The desktop to set.
	 */
	public void setDesktop(DesktopPostp desktop) {
		this.desktop = desktop;
		this.intFrame = (InterfacePostp)this.desktop.getDesktop().getSelectedFrame();
	}
	
	/**
	 * The method return the postpModel.
	 * @return Returns The postpModel.
	 */
	public PostpModel getPostpModel() {
		return postpModel;
	}
	
	/**
	 * The method set the field postpModel from the parameter postpModel.
	 * @param postpModel The postpModel to set.
	 */
	public void setPostpModel(PostpModel postpModel) {
		this.postpModel = postpModel;
	}
	
	/**
	 * The method return the stePostp.
	 * @return Returns The stePostp.
	 */
	public StepPostp getStePostp() {
		return stepPostp;
	}
	
	/**
	 * The method set the field stePostp from the parameter stePostp.
	 * @param stePostp The stePostp to set.
	 */
	public void setStePostp(StepPostp stePostp) {
		this.stepPostp = stePostp;
	}
	
//	*********************************************************************************************	
}//end of this class




